package com.shihb.homework

import scala.collection.mutable
import scala.collection.mutable.ArrayBuffer

/**
 * Description:
 *
 * @author shihb
 * @date 2020/9/29 23:40
 */
object CollectionHomework {
  def main(args: Array[String]): Unit = {
    test2

  }
  def test1: Unit ={
    /*
    将AAABBBCCCDDD用fold放入一个ArrayBuffer
     */
    val str = "AAABBBCCCDDD"
    val chars: ArrayBuffer[Char] = ArrayBuffer[Char]()
//    str.fold('s')((o1,o2)=>{
//      chars.append(o2)
//      o1
//    })
    str.foldLeft(chars)(putChar)
    println(chars)

  }
  def putChar(arr:ArrayBuffer[Char],c:Char): ArrayBuffer[Char] ={
    arr.append(c)
    arr
  }

  def test2: Unit ={
    /*
    将AAABBBBBCCCCCDDCD字母计数返回Map
    */
    val str = "AAABBBBBCCCCCDDCD"
    val map: mutable.Map[Char, Int] = mutable.Map[Char, Int]()
    println(str.foldLeft(map)(charCount))

  }
  def charCount(map:mutable.Map[Char, Int],c:Char): mutable.Map[Char, Int] ={
    map+=(c->(map.getOrElse(c,0)+1))

  }



}
